﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Diagnostics;
using System.ComponentModel;
using WPFExcelReport.Helpers;
using System.Windows.Input;
namespace WPFExcelReport
{
    public partial class TWT15
    {
        public TWT15()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWTSAER();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            Pattern();
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["WTBMANDANT"] = app.Mandant;
            dwData["WTBSRFNR"] = " AND WTBSRFNR LIKE '%" + ART + "%'";
            dwData["WTBCADKEY1"] = " AND WTBCADKEY1 LIKE '%" + FARBE.Trim() + "%'";
            SResult = TMV.QWT03(dwData);
            MergeData(SResult, false);
            TBDetail.Clear();
            base.QueryCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            IsSaved = false;
            base.EditCommand(sender, e);
        }
        public void Pattern()
        {
            if (dwMaster == null)
            {
                return;
            }
            TMV.Pattern(dwMaster);

        }

        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            if (!IsSaved)
                return;
            if (ORDERNUM.Length < 15)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["WTBMANDANT"] = app.Mandant;
            dwData["WTBSRFNR"] = " AND WTBSRFNR = '" + ORDERNUM.Substring(0, 15) + "'";
            SResult = TMV.QWT03(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            IsSaved = false;
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateWTSAER(dwMaster, 0);
            base.SaveCommand(sender, e);
            /*
            vwMaster.View.MoveCurrentTo(-1);
            TBDetail.Clear();*/
        }

        public void RefreshCommand(object sender, RoutedEventArgs e)
        {

            TMV.BuidWTSAER(dwMaster);//写入Table 
            Pattern();//计算根数 
            TMV.BuidWTSAER(dwMaster);//写回table 
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                return;
            }
            filter = "WTBSRFNR='" + dwMaster["WTBSRFNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                TMV.DWT01(row);
                row.Delete();
            }
            vwMaster.View.MoveCurrentTo(-1);
            base.DeleteCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("清先选择一个记录");
                return;
            }
            if (!IsCopy)
            {
                IsCopy = true;
                ART = "";
                VAR = dwMaster["WTBANZFARB"].ToString();
                FARBE = dwMaster["WTBCADKEY1"].ToString();
                return;
            }
            if (VAR == "0")
            {
                MessageBox.Show("清先输入颜色数");
                return;
            }
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;

            dwData["WTBSRFNR"] = ART.Trim().PadRight(15);
            dwData["WTBANZFARB"] = VAR;
            dwData["WTBCADKEY1"] = FARBE.Trim().PadRight(13);
            for (int i = int.Parse(VAR); i < int.Parse(dwMaster["WTBANZFARB"].ToString()); i++)
            {
                dwData["WTBGARN" + i] = 0;
                dwData["WTBFDZ" + i] = 0;
                dwData["WTBFNR" + i] = 0;
                dwData["WTBANZFAE" + i] = 0;
            }
            for (int i = int.Parse(dwMaster["WTBANZFARB"].ToString())+1; i <= int.Parse(VAR); i++)
            {
                dwData["WTBGARN" + i] = TMV.WA[i - 1];
                dwData["WTBFDZ" + i] = 1;
                dwData["WTBFNR" + i] = i;
                dwData["WTBANZFAE" + i] = 1;
            }
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertWTSAER(dwData, app.company);
            IsCopy = false;
            ART = "";
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                ART = "";
                VAR = "0";
                FARBE = "J".PadRight(13);
                return;
            }
            if (VAR == "0")
            {
                MessageBox.Show("清先输入颜色数");
                return;
            }
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["WTBSRFNR"] = ART.Trim().PadRight(15);
            dwData["WTBANZFARB"] = VAR;
            dwData["WTBCADKEY1"] = FARBE.Trim().PadRight(13);
            for (int i = 1; i <= int.Parse(VAR); i++)
            {
                dwData["WTBGARN" + i] = TMV.WA[i - 1];
                dwData["WTBFDZ" + i] = 1;
                dwData["WTBFNR" + i] = i;
                dwData["WTBANZFAE" + i] = 1;
            }
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertWTSAER(dwData, app.company);
            IsNew = false;
            ART = "";
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }

        public override void OnCADPATTERNChg()
        {
            TBDetail.Clear();
            if (CADPATTERN.Length < 5)
                return;
            TMV.JLTPattern(CADPATTERN);
            base.OnCADPATTERNChg();
        }
        public override void OnREGULATORChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (REGULATOR)
            {
                TMV.WTBCADKEY2 = "J".PadRight(6);
            }
            else
            {
                TMV.WTBCADKEY2 = " ".PadRight(6);
            }
            base.OnREGULATORChg();
        }

    }
}
